package net.sourceforge.j2meautonetwork.view;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;

import net.sourceforge.j2meautonetwork.J2meAutoNetworkMIDlet;
import net.sourceforge.j2meautonetwork.model.User;
import net.sourceforge.j2meautonetwork.operation.NetworkManager;
import net.sourceforge.j2meautonetwork.util.CodeDefine;
import net.sourceforge.j2meautonetwork.util.Utilities;

/**
 * 主窗体,您可以修改此窗体为网络连接设置窗体.
 * @author Steven King
 * @version 0.1
 */

public class MainForm extends Form implements CommandListener, Runnable {

    private Command Test = new Command(CodeDefine.TEST_COMMAND_LABLE,
            Command.OK, 0);

    private Command Exit = new Command(CodeDefine.EXIT_COMMAND_LABLE,
            Command.EXIT, 0);

    private static MainForm instance = null;

    public static synchronized MainForm getMainForm() {
        if (instance == null) {
            instance = new MainForm(CodeDefine.TEST_FORM_TITLE_LABLE);
            instance.initSelf();
        }
        return instance;
    }

    public MainForm(String title) {
        super(title);
    }

    private void initSelf() {
        this.append(CodeDefine.TEST_FORM_PAGE_CONTENT);
        this.addCommand(Test);
        this.addCommand(Exit);
        this.setCommandListener(this);
    }

    public void commandAction(Command command, Displayable displayable) {
        if (command == Test) {
            new Thread(this).start();
            Utilities.debug("开始测试!!!!");
            Utilities.debug("当前模式: " + (User.getUser().isProxy() ? "CMWAP" : "CMNET"));
        } else if (command == Exit) {
            J2meAutoNetworkMIDlet.getMIDlet().notifyDestroyed();
        }
    }

    public void run(){
        if (NetworkManager.getInstance().tryAllTest()) {
            Utilities.debug("连接成功,网络连接点: " + (User.getUser().isProxy() ? "CMWAP" : "CMNET"));
        } else {
            Utilities.debug("连接失败,如果三星或者国内的手机,请重新启动后再试.请不要禁用网络连接!");
        }
    }

}
